package com.example.user.userinfo

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.mvvm.BaseActivity
import com.example.user.R
import com.example.user.databinding.ActivityUpdatePwdBinding

@Route(path = "/user/UpdatePwdActivity")
class UpdatePwdActivity : BaseActivity<ActivityUpdatePwdBinding,UserInfoViewModel>() {
    override fun initData() {
        viewModel.sendCodeSuccess.observe(this){
            if(it.code==200){
                binding.etCode.setText(it.data.content.substring(8,14))
            }
        }
        viewModel.updatePwdSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                finish()
            }
        }
    }

    override fun initView() {
        //展示用户手机号  但手机号中间四位加密
        val phone = SPUtils.getInstance().getString("phone")
        binding.tvPhone.text = "当前手机号:${phone.replaceRange(3,7,"****")}"
        //点击验证码 获取验证码
        binding.btnGetcode.setOnClickListener {
            viewModel.sendCode(phone)
        }
        binding.btnSave.setOnClickListener {
            //非空检验  两次密码是否一致  密码正则是否正确
            viewModel.updatePwd(phone,binding.etCode.text.toString(),binding.etPwd.text.toString())

        }
    }

    override fun getViewModel(): Class<UserInfoViewModel> = UserInfoViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_update_pwd

}